package com.github.jiangxch.rpc.transport.exchanger.jdk;

import com.github.jiangxch.rpc.serialization.Serializer;
import com.github.jiangxch.rpc.transport.ChannelHandler;
import com.github.jiangxch.rpc.transport.Client;
import com.github.jiangxch.rpc.transport.Server;
import com.github.jiangxch.rpc.transport.Transporter;
import com.github.jiangxch.rpc.transport.exchanger.ExchangeChannelHandler;
import com.github.jiangxch.rpc.transport.exchanger.ExchangeClient;
import com.github.jiangxch.rpc.transport.exchanger.ExchangeServer;
import com.github.jiangxch.rpc.transport.exchanger.Exchanger;
import com.github.jiangxch.rpc.transport.exchanger.Replier;

import java.net.InetSocketAddress;

/**
 * @author: jiangxch
 * @date: 2021/2/26 上午1:52
 */
public class JdkExchanger implements Exchanger {
    
    @Override
    public ExchangeServer bind(InetSocketAddress address, Serializer serializer, Transporter transporter,Replier replier) {
        Server server = transporter.bind(address, new JdkExchangeChannelHandler(replier), serializer);
        return new JdkExchangeServer(server);
    }
    
    @Override
    public ExchangeClient connect(InetSocketAddress address, Serializer serializer,
            Transporter transporter) {
        Client client = transporter.connect(address, new JdkExchangeChannelHandler(null), serializer);
        return new JdkExchangeClient(client);
    }
}
